package com.annoncesautomobile.vue.autres;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.annoncesautomobile.R;
import com.annoncesautomobile.modele.concreteEntities.Alerte;
import com.annoncesautomobile.modele.local.MesFavoris;
import com.annoncesautomobile.modele.local.MonJeton;
import com.annoncesautomobile.modele.util.IModelXML;
import com.annoncesautomobile.modele.util.ModelXML;
import com.annoncesautomobile.vue.SuperActivity;
import com.annoncesautomobile.vue.MainActivity;
import com.annoncesautomobile.vue.adapters.ListeBoutonBas;

public class AutresActivity extends SuperActivity {

	private IModelXML model;
	
	private ProgressDialog progressDialog;

	private Set<Button> buttons;
	private Button buttonFavoris;
	private Button buttonAlertes;
	private Button buttonCredits;
	
	private List<Alerte> alertes;

	private TextView pastilleFavoris;
	private TextView pastilleAlertes;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_autres);

		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
		TextView textViewActivityTitleText = (TextView) findViewById(R.id.activity_title_text);
		textViewActivityTitleText.setText(getString(R.string.title_activity_autres));

		ListeBoutonBas listeBoutonBas = new ListeBoutonBas(this);
		LinearLayout lt = (LinearLayout) this.findViewById(R.id.mainLayoutAutres);
		lt.addView(listeBoutonBas.getView());
		
		this.progressDialog = new ProgressDialog(this);
		this.progressDialog.setMessage("Chargement...");

		this.model = ModelXML.getInstance();

		this.buttons = new HashSet<Button>();
		this.buttonFavoris = (Button) this.findViewById(R.id.buttonMesFavoris);
		this.buttons.add(this.buttonFavoris);
		this.buttonFavoris.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				// Lancement mes favoris
				Intent intent = new Intent(AutresActivity.this, MesFavorisActivity.class);
				startActivity(intent);
			}
		});

		this.buttonAlertes = (Button) this.findViewById(R.id.buttonMesAlertes);
		this.buttons.add(this.buttonAlertes);
		this.buttonAlertes.setOnClickListener(new OnClickListener() {		
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(AutresActivity.this, MesAlertesActivity.class);
				startActivity(intent);
			}
		});
		
		this.buttonCredits = (Button) this.findViewById(R.id.buttonCredit);
		this.buttons.add(this.buttonCredits);
		this.buttonCredits.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(AutresActivity.this, CreditsActivity.class);
				startActivity(intent);
			}
		});
		this.setButtonsHeight();

		this.pastilleFavoris = (TextView) this.findViewById(R.id.pastilleMesFavoris);
		this.pastilleAlertes = (TextView) this.findViewById(R.id.pastilleMesAlertes);

	}

	public void onBackPressed(View view) {
		this.onBackPressed();
	}

	@Override
	public void onBackPressed() {
		//super.onBackPressed();
		Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(myIntent);
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		this.setButtonsHeight();
	}

	protected void setButtonsHeight() {
		for(Button button : this.buttons) {
			button.setHeight(button.getWidth());
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.loadContent();
	}

	protected void loadContent(){
		Set<Long> mesFaforis = MesFavoris.loadMesFavoris(getApplicationContext());
		this.pastilleFavoris.setText(String.valueOf(mesFaforis.size()));
		new LoadDataAlerts().execute();
	}
	
	protected void displayContent() {
		this.pastilleAlertes.setText(Integer.valueOf(this.alertes.size()).toString());
	}

	private class LoadDataAlerts extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			if(!progressDialog.isShowing()) {
				progressDialog.show();
			}
		}

		@Override
		protected Void doInBackground(Void... params) {
			synchronized(AutresActivity.this) {
				alertes = AutresActivity.this.model.getListeAlertes(MonJeton.getRegistrationId(getApplicationContext()));
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void p) {
			if(progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			AutresActivity.this.displayContent();
		}

	}
}
